/**
 * Created by Administrator on 2015/12/9.
 */


var ISearcher = require('../api/service').ISearcher
var IGoods = require('../api/service').IGoods
var Page = require('../domain/domain').Page
var utils = require('utility')

module.exports = function (router) {

    router.get('/push/data', function *() {
        var $self = this
        var results = yield $self.db('SELECT * FROM Recipe').list()
        for(var i in results){
            yield IGoods.saveToSolr(JSON.stringify({id:results[i].id, title:results[i].title, type:1}))
        }
        results = yield $self.db('SELECT * FROM Menu').list()
        for(var i in results){
            yield IGoods.saveToSolr(JSON.stringify({id:results[i].id, title:results[i].name, type:2}))
        }
        this.body = {
            code: 200
        }
    })

    router.get('/suggest', function *() {
        var $self = this
        var field = $self.query.field
        console.log('suggest field is ', field)
        var results = yield ISearcher.suggest(field, 1)
        console.log('------------------->' + JSON.stringify(results))
        var suggests = []
        for(var i in results){
            var item = results[i].split(',')
            suggests.push({field:item[0], count:item[1]})
        }
        this.body = {
            code:200,
            suggests: suggests
        }
    })

    /**
     * =========全文检索=========
     */
    router.get('/searcher', function *() {
        var $self = this
        var pageNum = this.query.pageNum || 1
        var qr = $self.query.qr
        if(!qr){
            $self.response.redirect('/')
            return
        }
        var data = yield ISearcher.searcher(qr, pageNum)
        data = JSON.parse(data)
        var results = data.results
        for(var i in results){
            switch(results[i].type||0){
                case 1:
                    var recipe = yield $self.db('SELECT * FROM Recipe WHERE id=?', results[i].id).unique()
                    recipe.highlightTitle = results[i].title
                    recipe.type = results[i].type
                    results[i] = recipe
                    break;
                case 2:
                    var sqls = [
                        'SELECT M.id, M.name AS title, M.upvote, M.createOn, M.comment, R.finishedProductImage AS image',
                        'FROM (SELECT * FROM Menu WHERE id=?) M',
                        'LEFT JOIN Recipe_Menu RM ON RM.m_id=M.id',
                        'LEFT JOIN Recipe R ON RM.r_id=R.id',
                        'GROUP BY M.id'
                    ]
                    var menu = yield $self.db(sqls.join(' '), results[i].id).unique()
                    menu.highlightTitle = results[i].title
                    menu.type = results[i].type
                    results[i] = menu
                    break;
                default:
                    results = []
            }
        }
        var page = new Page(pageNum, 12, data.count, results)
        console.log(JSON.stringify(results))
        yield $self._render('searcher', {
            title: '',
            qr: qr,
            page:page,
            dateformat: utils.YYYYMMDD
        })
    })

}